package com.suse.lhw.hms.authorizaiontserver.config;


import com.suse.lhw.hms.authorizaiontserver.dao.UUserMapper;
import com.suse.lhw.hms.authorizaiontserver.domin.UUser;
import com.suse.lhw.hms.authorizaiontserver.service.URoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ProjectName: health_manage_system
 * @Package: com.suse.lhw.security.service.Impl
 * @ClassName: MyUserDetailService
 * @Description: java类作用描述
 * @Author: 李鸿炜
 * @CreateDate: 2021/1/31 19:28
 * @UpdateUser: 更新者
 * @UpdateDate: 2021/1/31 19:28
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Service("userDetailsService")
public class MyUserDetailService implements UserDetailsService {
    @Autowired
    UUserMapper uUserMapper;
    @Autowired
    URoleService uRoleService;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //通过传来的username(这个username指id)找user
        UUser uUser = uUserMapper.selectByPrimaryKey(s);
        if (uUser == null) {
            System.out.println("?????");
            throw new UsernameNotFoundException("用户名不存在");
        }
        System.out.println(uUser.toString());
        //通过id找权限
        List<GrantedAuthority> authorities = uRoleService.selectRoleListByUserId(s);
//        List<GrantedAuthority> authorities = AuthorityUtils.commaSeparatedStringToAuthorityList("roles");
        return new User(uUser.getUserId(), uUser.getPassword(), authorities);
    }
}
